package com.hotacorp.opencloud.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.hotacorp.opencloud.gateway.filter.factory.CompositeRateLimiterGatewayFilterFactory;
import com.hotacorp.opencloud.gateway.keyresolver.ClientKeyResolver;
import com.hotacorp.opencloud.gateway.keyresolver.MethodKeyResolver;
import com.hotacorp.opencloud.gateway.keyresolver.OriginKeyResolver;
import com.hotacorp.opencloud.gateway.keyresolver.UserKeyResolver;

/**
 * 自定义限流过滤器配置器，可以组合多个条件进行限流
 * @author lwg
 *
 */
@Configuration
public class RateLimiterConfig {

	@Bean
	public CompositeRateLimiterGatewayFilterFactory compositeRateLimiterGatewayFilterFactory(RateLimiter<?> rateLimiter) {
		return new CompositeRateLimiterGatewayFilterFactory(rateLimiter);
	}

	@Bean(name = ClientKeyResolver.BEAN_NAME)
	public ClientKeyResolver clientKeyResolver() {
		return new ClientKeyResolver();
	}

	@Bean(name = MethodKeyResolver.BEAN_NAME)
	public MethodKeyResolver methodKeyResolver() {
		return new MethodKeyResolver();
	}

	@Bean(name = OriginKeyResolver.BEAN_NAME)
	@Primary
	public OriginKeyResolver originKeyResolver() {
		return new OriginKeyResolver();
	}

	@Bean(name = UserKeyResolver.BEAN_NAME)
	public UserKeyResolver userKeyResolver() {
		return new UserKeyResolver();
	}

}
